//
//  EmployeeStasticViewController.swift
//  HuaYun
//
//  Created by Mac on 2019/4/12.
//  Copyright © 2019 xyxorigation. All rights reserved.
//

import UIKit
import RxSwift
class EmployeeStasticViewController: UITableViewController,UIPickerViewDelegate,UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 3
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return 0
    }
    

    let segcontrol = AccompanySegmentControl.init(items: ["用工类型","组织架构"])
    
    let picker = UIPickerView.init(frame: CGRect.init(origin: .zero, size: CGSize(width: UIScreen.main.bounds.size.width, height: 220)))
    
    let field = UITextField()
    
    let toolbarview = UIView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
//        self.field.inputAccessoryViewController = UIInputViewController()
        self.view.addSubview(field)
        self.field.inputView = picker
        self.field.inputAccessoryView = self.toolbarview
        self.picker.backgroundColor = .white
        self.picker.delegate = self
        self.picker.dataSource = self
        
        
        self.tableView.backgroundColor = UIColor.dftBack
        segcontrol.selectedSegmentIndex = 0
        segcontrol.tintColor = UIColor.clear
        segcontrol.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white,NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16)], for: .selected)
        segcontrol.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.init(red: 1, green: 1, blue: 1, alpha: 0.5),NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16)], for: .normal)
        segcontrol.addTarget(self, action: #selector(change(seg:)), for: .valueChanged)
        self.navigationItem.titleView = segcontrol
        
        
        toolbarview.frame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: UIScreen.main.bounds.size.width, height: 40))
        toolbarview.layer.borderWidth = 0.5
        toolbarview.layer.borderColor = UIColor.hex("999999").cgColor
        let cancelButton = UIButton.init(type: .system)
        cancelButton.setTitleColor(UIColor.black, for: .normal)
        cancelButton.frame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: 100, height: 40))
        cancelButton.setTitle("取消", for: .normal)
        cancelButton.addTarget(self, action: #selector(canceledit), for: .touchUpInside)
        toolbarview.addSubview(cancelButton)
        let sureButton = UIButton.init(type: .system)
        sureButton.setTitleColor(UIColor.black, for: .normal)
        sureButton.frame = CGRect.init(origin: CGPoint.init(x: UIScreen.main.bounds.size.width - 100, y: 0), size: CGSize.init(width: 100, height: 40))
        sureButton.setTitle("确定", for: .normal)
        sureButton.addTarget(self, action: #selector(endedit), for: .touchUpInside)
        toolbarview.addSubview(sureButton)
        toolbarview.backgroundColor = UIColor.white
        
        self.tableView.register(UINib.init(nibName: "ChartDataCell", bundle: Bundle.main), forCellReuseIdentifier: "ChartDataCell")
        
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 44
    }
    
    let bag = DisposeBag()
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView()
        view.backgroundColor = UIColor.white
        if section == 0 {
            let label = UILabel()
            label.text = "在职员工"
            label.font = UIFont.systemFont(ofSize: 14)
            view.addSubview(label)
            label.snp.makeConstraints { (make) in
                make.left.equalToSuperview().offset(15)
                make.centerY.equalToSuperview()
            }
            let countLabel = UILabel()
            countLabel.text = "(合计:2877人)"
            countLabel.font = UIFont.systemFont(ofSize: 12)
            view.addSubview(countLabel)
            countLabel.snp.makeConstraints { (make) in
                make.left.equalTo(label.snp.right).offset(15)
                make.centerY.equalToSuperview()
            }
            
            let detailButton = UIButton.init(type: .system)
            detailButton.layer.borderWidth = 0.5
            detailButton.layer.borderColor = UIColor.black.cgColor
            detailButton.layer.cornerRadius = 3
            detailButton.setTitle("详情", for: .normal)
            detailButton.setTitleColor(.hex("333333"), for: .normal)
            detailButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
            view.addSubview(detailButton)
            detailButton.rx.tap
                .subscribe { (event) in
                    print("detail")
            }.disposed(by: self.bag)
            detailButton.snp.makeConstraints { (make) in
                make.right.equalToSuperview().offset(-15)
                make.centerY.equalToSuperview()
                make.width.equalTo(40)
                make.height.equalTo(20)
            }
        } else if section == 1 {
            let label = UILabel()
            label.text = "入职/入场人数"
            label.font = UIFont.systemFont(ofSize: 14)
            view.addSubview(label)
            label.snp.makeConstraints { (make) in
                make.left.equalToSuperview().offset(15)
                make.centerY.equalToSuperview()
            }
            let countLabel = UILabel()
            countLabel.text = "(合计:2877人)"
            countLabel.font = UIFont.systemFont(ofSize: 12)
            view.addSubview(countLabel)
            countLabel.snp.makeConstraints { (make) in
                make.left.equalTo(label.snp.right).offset(15)
                make.centerY.equalToSuperview()
            }
            
            let recentThreeButton = UIButton.init(type: .system)
            recentThreeButton.layer.borderColor = UIColor.black.cgColor
            recentThreeButton.layer.borderWidth = 0.5
            recentThreeButton.layer.cornerRadius = 3
            recentThreeButton.setTitle("近3月", for: .normal)
            recentThreeButton.setTitleColor(UIColor.hex("333333"), for: .normal)
            recentThreeButton.setTitleColor(UIColor.green, for: .selected)
            recentThreeButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
            view.addSubview(recentThreeButton)
            
            recentThreeButton.snp.makeConstraints { (make) in
                make.right.equalToSuperview().offset(-15)
                make.centerY.equalToSuperview()
                make.width.equalTo(60)
                make.height.equalTo(20)
            }
            
            recentThreeButton.rx.tap
                .subscribe { (event) in
                    print("recent")
            }.disposed(by: self.bag)
            
            let currentButton = UIButton.init(type: .system)
            currentButton.setTitle("本月", for: .normal)
            currentButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
            view.addSubview(currentButton)
            currentButton.snp.makeConstraints { (make) in
                make.right.equalTo(recentThreeButton.snp.left).offset(-15)
                make.centerY.equalToSuperview()
                make.width.equalTo(40)
                make.height.equalTo(20)
            }
            currentButton.setTitleColor(UIColor.hex("333333"), for: .normal)
            currentButton.setTitleColor(UIColor.green, for: .selected)
            currentButton.layer.borderColor = UIColor.black.cgColor
            currentButton.layer.borderWidth = 0.5
            currentButton.layer.cornerRadius = 3
            currentButton.rx.tap
                .subscribe { (event) in
                    print("recent")
                }.disposed(by: self.bag)
        } else {
            let label = UILabel()
            label.text = "离职/离场人数"
            label.font = UIFont.systemFont(ofSize: 14)
            view.addSubview(label)
            label.snp.makeConstraints { (make) in
                make.left.equalToSuperview().offset(15)
                make.centerY.equalToSuperview()
            }
            let countLabel = UILabel()
            countLabel.text = "(合计:2877人)"
            countLabel.font = UIFont.systemFont(ofSize: 12)
            view.addSubview(countLabel)
            countLabel.snp.makeConstraints { (make) in
                make.left.equalTo(label.snp.right).offset(15)
                make.centerY.equalToSuperview()
            }
            
            let recentThreeButton = UIButton.init(type: .system)
            recentThreeButton.layer.borderColor = UIColor.black.cgColor
            recentThreeButton.layer.borderWidth = 0.5
            recentThreeButton.layer.cornerRadius = 3
            recentThreeButton.setTitle("近3月", for: .normal)
            recentThreeButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
            view.addSubview(recentThreeButton)
            
            recentThreeButton.snp.makeConstraints { (make) in
                make.right.equalToSuperview().offset(-15)
                make.centerY.equalToSuperview()
                make.width.equalTo(60)
                make.height.equalTo(20)
            }
            
            recentThreeButton.rx.tap
                .subscribe { (event) in
                    print("recent")
                }.disposed(by: self.bag)
            recentThreeButton.setTitleColor(UIColor.hex("333333"), for: .normal)
            recentThreeButton.setTitleColor(UIColor.green, for: .selected)
            
            let currentButton = UIButton.init(type: .system)
            currentButton.setTitle("本月", for: .normal)
            currentButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
            view.addSubview(currentButton)
            currentButton.snp.makeConstraints { (make) in
                make.right.equalTo(recentThreeButton.snp.left).offset(-15)
                make.centerY.equalToSuperview()
                make.width.equalTo(40)
                make.height.equalTo(20)
            }
            currentButton.layer.borderColor = UIColor.black.cgColor
            currentButton.layer.borderWidth = 0.5
            currentButton.layer.cornerRadius = 3
            currentButton.setTitleColor(UIColor.hex("333333"), for: .normal)
            currentButton.setTitleColor(UIColor.green, for: .selected)
            currentButton.rx.tap
                .subscribe { (event) in
                    print("recent")
                }.disposed(by: self.bag)
        }
        
        return view
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 250
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return tableView.dequeueReusableCell(withIdentifier: "ChartDataCell")!
    }
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if let cell = cell as? ChartDataCell {
            cell.model = JSON()
        }
    }
    
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 20
    }
    
    
    @objc func endedit(){
        self.view.endEditing(true)
        
    }
    
    @objc func canceledit(){
        self.view.endEditing(true)
    }
    
    
    @IBAction func organizationShow(_ sender: Any) {
        self.field.becomeFirstResponder()
    }
    
    @objc func change(seg: UISegmentedControl){
        
    }
    
}
